﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Sale
{
    public partial class 原料挂帐情况 : System.Web.UI.Page
    {
        string sql =
       @"SELECT t1.ID,t1.编号,t1.年度,t1.季度,t2.品名 as 原料类别,t2.钢级,t2.规格,t3.名称 as 供货单位,cast(t1.数量 as float) as 数量,cast(t1.单价 as float) as 单价,cast(t1.数量 as float) *cast(t1.单价 as float) as 金额,t1.挂帐金额,发票金额,t1.签订日期,t1.是否完成
         FROM 采购原料合同 t1 left outer join 原料 t2 on t2.ID=t1.原料ID left outer join 原料供货单位 t3 on t3.ID=t1.供货ID";
      

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.SqlDataSource1.SelectCommand = sql + " order by t1.签订日期 desc";
                this.SqlText.Text = this.SqlDataSource1.SelectCommand;
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;         
           
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.SelectedID.Text = this.GridView1.SelectedDataKey[0].ToString();               

                
            }
            catch
            {
                this.GridView1.SelectedIndex = -1;
            }
        }
        private string GetState()
        {
            string state = "";

            if (this.BianHao.Text != "")
            {
                state += " and t1.编号 like '%" + this.BianHao.Text.Replace("'", "''").Replace("[", "[[]").Replace("%", "[%]") + "%'";
            }
            if (this.NianDu.Text != "0")
            {
                state += " and t1.年度=" + this.NianDu.Text;
            }
            if (this.JiDu.Text != "0")
            {
                state += " and t1.季度=" + this.JiDu.Text;
            }
            if (this.LeiBie.Text != "0")
            {
                state += " and t1.原料ID=" + this.LeiBie.Text;
            }
            if (this.GongHuo.Text != "0")
            {
                state += " and t1.供货ID=" + this.GongHuo.Text;
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string state = this.GetState();
            this.SqlText.Text = sql + state + " order by t1.签订日期 desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.BianHao.Text = "";
            this.NianDu.Text = "0";
            this.JiDu.Text = "0";
            this.LeiBie.Text = "0";
            this.GongHuo.Text = "0";
        }



        //合格判定
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            int k = 0;
            if (((CheckBox)sender).Checked) k = 1;
            this.SqlDataSource1.UpdateCommand = "update [采购原料合同] set [是否完成]=" + k + " where ID='" + ((CheckBox)sender).ToolTip+"'";
            this.SqlDataSource1.Update();
            this.GridView1.SelectedIndex = -1;       
        }
    }
}
